package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceimpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional  //事务
    public void savewithflavor(DishDTO dish) {
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dish,dish1);
        log.info("新增菜品");
        dishMapper.insert(dish1);
        //获取菜品id  用到了主键返回
        Long dishid = dish1.getId();

        List<DishFlavor> flavors = dish.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dishid);
            });
        dishFlavorMapper.insertBatch(flavors);
        }

    }

    @Override
    public PageResult page(DishPageQueryDTO dish) {
        PageHelper.startPage(dish.getPage(), dish.getPageSize());
        Page<DishVO> page=dishMapper.page(dish);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Transactional
    @Override
    public void delete(List<Long> dis) {
        //判断当前菜品是否能够删除，删除之前进行判断
        for (Long di : dis) {
            Dish dish = dishMapper.getById(di);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断是否与套餐表中的菜品关联 如果关联了，抛出异常
        List<Long> dishIdsBySetmealId = setmealDishMapper.getDishIdsBySetmealId(dis);
        if (dishIdsBySetmealId != null && dishIdsBySetmealId.size() > 0) {
            //当前菜品与套餐关联了
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品
//        for (Long di : dis) {
//            dishMapper.delete(di);
//            //删除菜品对应的口味
//            dishFlavorMapper.deleteByDishId(di);
//        }

        dishMapper.deletebach(dis);
        //删除菜品对应的口味
        dishFlavorMapper.deleteByDishIds(dis);


    }

    @Override
    public DishVO getByIdWithFlavor(Long id) {
        //根据id查询菜品
        Dish dish = dishMapper.getById(id);

        //根据id查询菜品对应的口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);


        //将查询到的菜品数据和对应的口味数据封装到vo
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish,dishVO);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    @Override
    public void updatewithflavor(DishDTO dish) {
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dish,dish1);
        //修改菜品表的基本信息
        dishMapper.update(dish1);

        //删除原有的口味数据
        dishFlavorMapper.deleteByDishId(dish1.getId());

        //重新插入口味数据
        List<DishFlavor> flavors = dish.getFlavors();
        if (flavors!= null && flavors.size() > 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(dish1.getId());
            });
            dishFlavorMapper.insertBatch(flavors);
        }


    }
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }


}
